<template>
  <div class="repeat-pwd">
    <div class="head">
      <a href="javascript:;" @click="backPrev">
        <i class="iconfont icon-fanhui"/>
      </a>修改密码
    </div>
    <form
      action=""
      class="form-style">
      <div class="form-group clearfix">
        <span class="fl">旧密码 :</span>
        <input
          v-model="validate.oldPwd"
          type="password"
          class="fl user"
          name="userName"
          placeholder="请输入旧密码"
          @keyup="oldPwd">
      </div>
      <div
        v-show="validateOldPwd"
        class="error">请输入6-16位密码</div>
      <div class="form-group clearfix">
        <span class="fl">新密码 :</span>
        <input
          v-model="validate.newPwd"
          type="password"
          class="fl pwd"
          name="passWord"
          placeholder="请输入新密码"
          @keyup="newPwd">
      </div>
      <div
        v-show="validateNewPwd"
        class="error">请输入6-16位密码</div>
      <a
        href="javascript:;"
        class="login-btn form-btn"
        @click="revampSbmit">修改</a>
    </form>
  </div>
</template>
<script>
export default {

  data () {
    return {
      validate: {
        oldPwd: '',
        newPwd: ''
      },
      validateOldPwd: false,
      validateNewPwd: false,
      isShowMask: false,
      num: 2

    }
  },
  methods: {
    backPrev () {
      this.$router.go(-1)
    },
    oldPwd () {
      const oldPwdVal = this.validate.oldPwd
      const regex = /^\w{6,16}$/
      if (regex.test(oldPwdVal)) {
        this.validateOldPwd = false
        return true
      } else {
        this.validateOldPwd = true
        return false
      }
    },
    newPwd () {
      const newPwdVal = this.validate.newPwd
      const regex = /^\w{6,16}$/
      if (regex.test(newPwdVal)) {
        this.validateNewPwd = false
        return true
      } else {
        this.validateNewPwd = true
        return false
      }
    },

    revampSbmit () {
      var isRepeatPwd = this.oldPwd() && this.newPwd()
      if (isRepeatPwd) {
        let token = sessionStorage.getItem('token')
        let userName = sessionStorage.getItem('userName')
        console.log(userName)
        this.$http.post(this.URL + '/user/updateUser', {
          token: token,
          userName: userName,
          passWord: this.validate.oldPwd,
          newPassWord: this.validate.newPwd
        }).then(function (res) {
          console.log(res)
          if (res.status === 200) {
            this.places = res.data.regionName
            this.$router.push('/')
          }
        })
      };
    }
  }
}

</script>
